// myPages/process/process.js
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    steps: [{
        text: '预约'
      },
      {
        text: '付款'
      },
      {
        text: '导师确认'
      },
      {
        text: '见面'
      },
      {
        text: '评价'
      }
    ],
    active: 0,
    userid: null, //用户id
    userOrderId: null, //订单编号
    orderInfo: {}, //订单详情
    myOptions: {}, //导师详情传来参数
    contantShow: true, //是否显示聊天
    isHidden: true, //是否显示修改  true为是
    orderId: -1, //修改订单ID,
    isPay: false, //是否去付款判斷小程序是否去支付頁面
    maskShow: true
  },
  onChange(event) {

  },
  /**获得预约第一步 */
  onGetApointment: function(e) {
    wx.setNavigationBarTitle({
      title: '提交订单' //页面标题
    });
    let that = this;
    //接收预约订单  
    let param = e.detail;
    //去掉表情
    param.question = this.filterEmoji(param.question);
    param.introduction = this.filterEmoji(param.introduction);
    param.userId = this.data.userid;
    console.log(param)
    util.request(api.UserConfirmOrder, param, "POST", true).then(function(res) {
      console.log(res)
      if (res.code === 200) {
        that.getOrderInfo(res.result);
        that.setData({
          active: 1,
          userOrderId: res, //返回订单唯一的id
          contantShow: true
        })
      }
    });
  },
  /**订单修改第一步 */
  onUpdateApointmentUpdate(e) {
    wx.setNavigationBarTitle({
      title: '提交订单' //页面标题
    });
    let that = this;
    //修改预约订单  
    let param = e.detail;
    //去掉表情
    param.question = this.filterEmoji(param.question);
    param.introduction = this.filterEmoji(param.introduction);
    param.userId = this.data.userid;
    util.request(api.UpdateUserOrder, param, "PUT", true).then(function(res) {
      if (res.code === 200) {
        that.getOrderInfo(param.id);
        that.setData({
          active: 1,
          userOrderId: res, //返回订单唯一的id
          contantShow: true
        })
      }
    });
  },
  //匹配表情
  filterEmoji(name) {
    var str = name.replace(/[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig, "(不支持表情)");
    return str;
  },
  /**获得付款第二步 */
  onGetPay: function(e) {
    wx.setNavigationBarTitle({
      title: '详情' //页面标题
    });
    // var that = this;
    // that.setData({
    //   active: 3
    // })
    this.setData({
      isPay: true
    })
  },
  /**获得确认第三部 */
  onGetYes() {
    var that = this;
    that.setData({
      active: 3
    })
  },
  /**订单完成 第四部 */
  finish() {
    var that = this;
    that.setData({
      active: 4
    })
  },
  /**
   * 获取订单信息
   * param 订单Id
   */
  getOrderInfo(orderNum) {
    let that = this;
    util.request(api.SelectUserOrderByOrderId, {
      id: orderNum
    }, 'GET').then(function(res) {
      if (res.code === 200) {
        let resStatus = res.result == null ? -1 : res.result.status;
        let status = -1
        switch (resStatus) {
          case 1: //未支付
            status = 1;
            break;
          case 2: //已支付
            status = 2;
            break;
          case 3: //失效订单
            status = 2;
            break;
          case 4:
            status = 3; //导师接单
            break;
          case 5:
            status = 3; //已交易
            break;
          case 6:
            status = 2; //取消订单
            break;
          case 7:
            status = 2; //退款订单
            break;
          case 8:
            status = 4; //导师接单
            break;
          default:
            status = -1;
        }
        if (status == -1) {
          wx.showModal({
            title: '错误提示',
            content: '找不到该订单状态，确定返回上一页',
            showCancel: false, //是否显示取消按钮
            complete: function(res) {
              wx.navigateBack({
                delta: 1
              })
            },
          })
        }
        that.setSteps(res.result.meet_type)
        //去掉富文本标签
        res.result.descrition = res.result.descrition.replace(/<[^>]+>/g, '');
        console.log(res.result)
        that.setData({
          orderInfo: res.result,
          active: status
        })
      }
    });
  },
  //跳转聊天页面
  gotoContant: function() {
    //若果没有订单id则不能跳转
    if (this.data.orderInfo.id == undefined) {
      wx.showModal({
        title: '提示',
        content: '订单没有生成，无法进入私信页面',
        showCancel: false
      })
      return false;
    }
    wx.navigateTo({
      url: '/myPages/contact/contact?orderId=' + this.data.orderInfo.id + "&to_user_id=" + this.data.orderInfo.to_user_id + "&user_id=" + this.data.orderInfo.user_id
    })
  },
  //设置进度调标题
  setSteps(str) {
    let new_array = this.data.steps
    if (str == 1) {
      new_array = [{
          text: '预约'
        },
        {
          text: '付款'
        },
        {
          text: '导师确认'
        },
        {
          text: '语音'
        },
        {
          text: '评价'
        }
      ]
    }
    this.setData({
      steps: new_array
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    let pages = getCurrentPages();
    if (pages[pages.length - 2] == undefined) {
      wx.switchTab({
        url: '/pages/index/index',
      });
      return false;
    }
    let prevPage = pages[pages.length - 2].route;
    const userInfo = wx.getStorageSync('userInfo');
    if (prevPage.indexOf('/teacher') > 0 && options.action == undefined) {
      //导师详情穿过来的页面进入预约页面
      if (userInfo != "" || userInfo != undefined) {
        that.setData({
          userid: userInfo.id,
        })
      }
      //设置订单详情
      let myOptions = Object.assign({}, options);
      this.setData({
        myOptions: myOptions,
      });
      //设置步骤标题
      this.setSteps(myOptions.type);
    } else if (options.action == 'update') {
      //修改预约切换组件
      that.setData({
        active: 0,
        orderId: options.orderId,
        userid: userInfo.id,
      })
    } else {
      //其他页面传过来的进入预约以外的页面
      this.getOrderInfo(options.userOrderId);
      if (JSON.stringify(this.data.orderInfo) == null) {
        wx.showModal({
          title: '提示',
          content: '获取不到订单信息',
          showCancel: false
        });
        return false;
      }
    }
    if (prevPage.indexOf('/orderTeacher') > 0) {
      that.setData({
        isHidden: false
      })
    } else {
      that.setData({
        isHidden: true
      })
    }
  },
  scrollToTop() {
    this.setData({
      scrollTop: 0,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.hideShareMenu();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    let pages = getCurrentPages();
    if (pages[pages.length - 1].route == "myPages/process/process" && pages[pages.length - 2].route == "myPages/teacher/teacher" && this.data.orderInfo.id != undefined && !this.data.isPay) {
      wx.redirectTo({
        url: '/myPages/process/process?action=' + 'update' + '&orderId=' + this.data.orderInfo.id,
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})